#include "illireg.h"

const char* programs[PROGRAM_COUNT] = {"listen", "flocking", "clocksync", "bullyalgo"};

const void* programsStart[PROGRAM_COUNT] = {start_listen, start_flocking, start_clocksync, start_bullyalgo}; //order is important, must match string names

void (*programStartPtr)(int argc, char* argv[]);

int main(int argc, const char* argv[])
{
	int i, j, done;
	done = 0;

	if (argc <= 1) {
		printf("Syntax: use %s\n\r", PROGRAM_OPTION_SHORT);
	}
	else {
		for (i = 0; i < argc && !done; i++ ) {
			//printf( "arg %d: %s\n", i, argv[i]);

			if (strcmp(argv[i], PROGRAM_OPTION_SHORT) == 0 || strcmp(argv[i], PROGRAM_OPTION_LONG) == 0) {
				if (i + 1 < argc) {
					i++;

					for (j = 0; j < PROGRAM_COUNT; j++) {
						if (strcmp(argv[i], programs[j]) == 0) {
							//printf("starting %s %d %d %d\n\r", programs[j], argc, i, argc - i);
							programStartPtr = programsStart[j];
							(*programStartPtr)(argc - (i), (char**)&argv[i]); //pass program name and rest of arguments
							done = 1;
						}
					}
				}
				else {
					printf("error: specify program: <");
					for (j = 0; j < PROGRAM_COUNT; j++) {
						printf("%s | ", programs[j]);
					}
					printf("> \n\r");
				}
			}
		}
	}

	return 0;
}
